package com.jt.demo51.pp03;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;

public class CglibProxy {
    public static Object getobject(Object target){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(getMethodInterceptor());
        return enhancer.create();
    }

    public static MethodInterceptor getMethodInterceptor(){
        return new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("数据库开始");
                Object o1 = methodProxy.invokeSuper(o, objects);
                System.out.println("数据库结束");
                return o1;
            }
        };
    }
}
